<?php

namespace common\models\member;

use common\enums\StatusEnum;
use Yii;
use yii\helpers\Html;
use common\helps\tools;
use common\models\backend\AdminBank;
use common\models\forms\CreditsLogForm;

/**
 * This is the model class for table "{{%recharge}}".
 *
 * @property integer $id
 * @property integer $member_id
 * @property integer $amount
 * @property string $username
 * @property integer $type
 * @property string $bank_name
 * @property integer $address
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $state
 * @property string $note
 * @property string $images
 * @property string $details
 */
class Recharge extends \yii\db\ActiveRecord
{
    public $username;
    public $bank_id;
    /**
     * 银行卡
     */
    const YINGHANGKA = 0;
    /**
     * 微信
     */
    const WEIXIN = 1;
    /**
     * 支付宝
     */
    const ZHIFUBAO = 2;
    public static $ActionType = [
        self::YINGHANGKA => '银行卡',
        self::WEIXIN => '微信',
        self::ZHIFUBAO => '支付宝',
    ];
    /**
     * 未通过
     */
    const WEITONGGUO = -1;
    /**
     * 未审核
     */
    const WEISHENHE = 0;
    /**
     * 未支付
     */
    const YIZHIFU = 1;
    /**
     * 已审核
     */
    const YISHENHE = 2;
    public static $tatusName = [
        self::WEITONGGUO => '未通过',
        self::WEISHENHE => '未审核',
        self::YIZHIFU => '未支付',
        self::YISHENHE => '已审核',
    ];

    public static function tableName()
    {
        return '{{%recharge}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {   
         $relues = [
            [['amount','bank_id', 'action_type'], 'required','on'=>'submit'],
            // [['bank_name','address'],'required'],
            [['member_id', 'created_at', 'updated_at', 'status','action_type'], 'integer'],
            [['details'], 'string'],
            [['address'], 'string', 'max' => 50],
            [['bank_name'], 'string', 'max' => 64],
            [['note', 'images','bank_user_name','order_sn', 'pay_img'], 'string', 'max' => 255],
            [['bank_id'],'checkBank']
        ];
        // $relues = array_merge($relues, );
        return $relues;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [

            'member_id' => 'User ID',
            'order_sn' => '订单编号',
            'amount' => '充值金额',
            'username' => '会员号',
            'bank_name' => '收款名称',
            'bank_user_name' => '收款人姓名',
            'address' => '收款账号',
            'created_at' => '提交时间',
            'updated_at' => '审核时间',
            'status' =>Yii::t('app', '状态'),
            'note' => '附言',
            'images' => 'images',
            'pay_img' => '打款凭证',
            'action_type' => '充值方式',
            'details'=>Yii::t('app','打回详情'),
        ];
    }

    public function getStatusName()
    {
        $list = self::getStates();
        return $list[$this->status] ?? '';
    }

    /*获取状态*/
    public static function getStates()
    {
        return [
            '未审核','未支付','已审核','审核未通过'
        ];  
    }
    public static function getActionTypes()
    {
        return [
            '会员充值','后台充值'
        ];  
    }
    public static function getActionType($type)
    {
        $states = self::getActionTypes();
        $class = ['info','success','danger'];
       
        return Html::tag('span',$states[$type],['class'=>'text text-'.$class[$type]]);
    }

    public function checkamount()
    {
        if ($this->amount<=0) {
            $this->addError('amount',yii::t('app','金额有误'));
        }
    }
   
    // 会员
    public function getMember()
    {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    public function checkBank()
    {
        $admin_bank = AdminBank::find()->where(['id'=>$this->bank_id,'state'=>StatusEnum::ENABLED, 'pay_type'=>$this->action_type])->one();
        if(!$admin_bank){
            return $this->addError('amount', yii::t('app', '收款银行有误'));
        }
        $this->bank_name =  $admin_bank->bank_name;
        $this->bank_user_name =  $admin_bank->bank_user_name;
        $this->address =       $admin_bank->bank_account;
        $this->images =       $admin_bank->bank_img;
    }

     /*执行充值*/
    public function ChongZhi()
    {   

        
        $this->status = 2;
        $this->updated_at=time();
        if ($this->update()) {
            $num = $this->amount;
            $remark = '充值';
            $action = 'incrMoney'; // 扣除余额
            
            Yii::$app->services->memberCreditsLog->$action(new CreditsLogForm([
                'member' => $this->member,
                'num' => $num,
                'credit_group' => '充值',
                'remark' => $remark,
            ]));
            return true;
        }else{
            throw new \Exception(yii::t('app','请重试'));
            
        }
    }

    public function beforeSave($insert)
    {   
        if (parent::beforeSave($insert)) {
            if($this->isNewRecord){  
                $this->order_sn = static::generateOrderNo('R');
            }
            return true;
        }else{
            return false;
        }
    }

    public static function generateOrderNo($prefix = '')
    {
        $danhao = date('Ymdhsi') . str_pad(mt_rand(1, 99999), 2, '0', STR_PAD_LEFT);
        return $prefix.$danhao;
    }
}
